<?php
/**
 * Created by PhpStorm.
 * 商品模型
 * User: caicm
 * Date: 2017/6/17
 * Time: 11:29
 */

namespace WMApi\Model;


use Think\Model;

class WmGoodsModel extends Model {

    /**
     * 根据商品菜单id获取商品数量
     * Created By caicm
     * @param int $menu_id
     * @return mixed
     */
    public function find_goodsnum_by_menu($menu_id=0){

        $where = array(
            'menu_id' => $menu_id,
            'is_del' => 0
        );

        return $this->where($where)->count('goods_id');

    }

    /**
     * 添加商品
     * Created By caicm
     * @param int $seller_id 商家id
     * @param int $store_id 店铺id
     * @param string $goods_name 商品名称
     * @param int $cat_id 分类id
     * @param int $cat_two_id 二级分类id
     * @param int $cat_one_id 一级分类id
     * @param int $menu_id 菜单id
     * @param int $goods_price 商品价格
     * @param int $unit_id 单位id
     * @param string $unit_name 单位名称
     * @param string $goods_img 图片地址
     * @param int $state 商品状态说明：0：未上架  1：上架 2：下架  3：平台强制下架
     * @param int $sort 排序
     * @param string $describe 描述
     * @param int $is_sku 是否有多规格 1是，0否
     * @param int $goods_weight 商品重量
     * @return mixed
     */
    public function add_goods($seller_id=0, $store_id=0, $goods_name='', $cat_id=0, $cat_two_id=0, $cat_one_id=0, $menu_id=0, $goods_price=0, $unit_id=0, $unit_name='', $goods_img='', $state=0, $sort=0, $describe='', $is_sku=0, $goods_weight=0){

        $data = array(
            'seller_id' => $seller_id,
            'store_id' => $store_id,
            'goods_name' => $goods_name,
            'cat_id' => $cat_id,
            'cat_two_id' => $cat_two_id,
            'cat_one_id' => $cat_one_id,
            'menu_id' => $menu_id,
            'goods_price' => $goods_price,
            'unit_id' => $unit_id,
            'unit_name' => $unit_name,
            'goods_img' => $goods_img,
            'state' => $state,
            'sort' => $sort,
            'describe' => $describe,
            'is_sku' => $is_sku,
            'goods_weight' => $goods_weight,
            'goods_sn' => get_goods_sn(),
            'add_time' => gmtime()
        );

        return $this->data($data)->add();

    }

    /**
     * 编辑商品
     * Created By caicm
     * @param int $goods_id 商品id
     * @param int $seller_id 商家id
     * @param int $store_id 店铺id
     * @param string $goods_name 商品名称
     * @param int $cat_id 分类id
     * @param int $cat_two_id 二级分类id
     * @param int $cat_one_id 一级分类id
     * @param int $menu_id 菜单id
     * @param int $goods_price 商品价格
     * @param int $unit_id 单位id
     * @param string $unit_name 单位名称
     * @param string $goods_img 图片地址
     * @param int $state 商品状态说明：0：未上架  1：上架 2：下架  3：平台强制下架
     * @param int $sort 排序
     * @param string $describe 描述
     * @param int $is_sku 是否有多规格 1是，0否
     * @param int $goods_weight 商品重量
     * @return mixed
     */
    public function edit_goods($goods_id=0, $seller_id=0, $store_id=0, $goods_name='', $cat_id=0, $cat_two_id=0, $cat_one_id=0, $menu_id=0, $goods_price=0, $unit_id=0, $unit_name='', $goods_img='', $state=0, $sort=0, $describe='', $is_sku=0, $goods_weight=0){

        $where = array(
            'seller_id' => $seller_id,
            'store_id' => $store_id,
            'goods_id' => $goods_id
        );

        $data = array(
            'goods_name' => $goods_name,
            'cat_id' => $cat_id,
            'cat_two_id' => $cat_two_id,
            'cat_one_id' => $cat_one_id,
            'menu_id' => $menu_id,
            'goods_price' => $goods_price,
            'unit_id' => $unit_id,
            'unit_name' => $unit_name,
            'goods_img' => $goods_img,
            'state' => $state,
            'sort' => $sort,
            'describe' => $describe,
            'is_sku' => $is_sku,
            'goods_weight' => $goods_weight,
            'update_time' => gmtime()
        );
        if($goods_img=='') unset($data['goods_img']);

        return $this->where($where)->data($data)->save();

    }

    /**
     * 获取详情详情
     * Created By caicm
     * @param int $seller_id
     * @param int $store_id
     * @param int $goods_id
     * @return mixed
     */
    public function find_goods_info($seller_id=0, $store_id=0, $goods_id=0){

        $where = array(
            'g.seller_id' => $seller_id,
            'g.store_id' => $store_id,
            'g.goods_id' => $goods_id,
        );

        $field = '
        g.goods_id,
        g.goods_name,
        g.goods_sn,
        g.is_sku,
        g.cat_id,
        c.cat_name,
        g.cat_one_id,
        g.cat_two_id,
        g.menu_id,
        m.menu_name,
        g.goods_price,
        g.discount,
        g.state,
        g.sort,
        g.goods_weight,
        g.unit_id,
        g.unit_name,
        g.goods_img,
        g.describe,
        g.is_del';

        $join = 'left join sdx_wm_category as c on g.cat_id = c.cat_id';
        $join .= ' left join sdx_wm_store_customize_menu as m on g.menu_id = m.menu_id';
        return $this->alias('g')->join($join)->field($field)->where($where)->find();

    }

    /**
     * 根据商品自定义菜单获取商品列表
     * Created By caicm
     * @param int $menu_id
     * @return mixed
     */
    public function find_goodslist_by_menuid($menu_id=0){

        $where = array(
            'menu_id' => $menu_id,
            'is_del' => 0
        );

        $field = 'goods_id,goods_name,goods_sn,is_sku,cat_id,menu_id,goods_price,state,unit_name,goods_img';

        return $this->field($field)->where($where)->order('sort desc,goods_id desc')->select();
    }

    /**
     * 商品状态修改
     * Created By caicm
     * @param int $goods_id
     * @param int $state
     * @param int $is_del
     * @return bool
     */
    public function update_goods_state($goods_id=0, $state=0, $is_del=0){

        $where = array(
            'goods_id' => $goods_id
        );

        $data = array(
            'state' => $state,
            'is_del' => $is_del,
            'update_time' => gmtime(),
        );
        if($state==0) unset($data['state']);
        if($is_del==0) unset($data['is_del']);

        return $this->where($where)->data($data)->save();

    }

}